package com.java8;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class Test {
    public static void main(String[] args) {
        //获取一个Stream流
        Stream<String> a = Stream.of("A", "b", "c","B","c");
        List<Integer> list=new ArrayList<>();
        list.add(3);
        list.add(4);
        list.add(1);
        list.add(2);

        Stream<Integer> stream = list.stream();
        List<User> users = User.users();
        Stream<User> usersSteam = users.stream();

        //forEach遍历
//        a.forEach(b-> System.out.println(b));
        //filter过滤
//        List<String> collect = a.filter(b -> {
//            return b.equals("A");
//        }).collect(Collectors.toList());
//        collect.forEach(b-> System.out.println(b));
//        //distinct去重
//        List<String> collect = a.distinct().collect(Collectors.toList());
//        collect.forEach(i-> System.out.println(i));
        //limit截取
//        List<String> collect = a.limit(2).collect(Collectors.toList());
//        collect.forEach(i-> System.out.println(i));
        //skip跳过
//        List<String> collect = a.skip(2).collect(Collectors.toList());
//        collect.forEach(i-> System.out.println(i));
        //排序sorted
//        List<String> collect = a.sorted().collect(Collectors.toList());
//        collect.forEach(i-> System.out.println(i));
        //最值max,min
//        String s = a.min(Comparator.comparing(i -> i)).get();
//        System.out.println(s);
        //统计reduce
//        Integer reduce = stream.reduce(0, (i, j) -> i + j);
//        System.out.println(reduce);
        //List结构转换Map结构
//        Map<Integer, User> collect = usersSteam.collect(Collectors.toMap(User::getId, user -> user));
//        for (Map.Entry<Integer, User> entry : collect.entrySet()) {
//            System.out.println(entry.getValue().getName());
//        }

        //List对象转List
//        List<String> collect = usersSteam.map(User::getName).collect(Collectors.toList());
//        collect.forEach(i-> System.out.println(i));
        //anyMatch匹配
//        boolean b = usersSteam.anyMatch(user -> user.getAge() > 100);
//        System.out.println(b);
        //allMatch匹配
//          boolean b = usersSteam.allMatch(user -> user.getAge() > 100);
//          System.out.println(b);
        //ﬁndAny查找
//        User user = usersSteam.findAny().get();
//        System.out.println(user.getName());
        //收集齐中joining() 方法
//        String collect = a.collect(Collectors.joining(",", "[", "]"));
//        System.out.println(collect);

        //对集合按照单个属性分组、分组计数、排序Collectors.groupingBy（）
//        Map<String, List<String>> collect = a.collect(Collectors.groupingBy(Function.identity()));
//        System.out.println(collect);
        //分组计数
//        Map<String, Long> collect = a.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
//        System.out.println(collect);
    }
}
